package com.joinlabs.action;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.joinlabs.entity.Address;
import com.joinlabs.entity.Enshrine;
import com.joinlabs.entity.User;
import com.joinlabs.entity.Wares;
import com.joinlabs.service.IUserService;
import com.joinlabs.util.MD5Utils;

@Controller
@RequestMapping("/user")
@SessionAttributes("user")
public class UserAction {
	
	@Resource
	private IUserService userServiceImpl;
	
	@RequestMapping("/userinfo")
	//显示个人信息界面
	public ModelAndView showInfo() {
		
		return new ModelAndView("/user/userinfo");
	}
	
	@RequestMapping("/showupdate")
	//显示修改信息界面
	public ModelAndView showUpdate() {
		
		return new ModelAndView("/user/updateinfo");		
	}
	
	@RequestMapping("/userimg")
	//修改头像照片界面
	public ModelAndView userImg() {
		
		return new ModelAndView("/user/userimg");		
	}
	
	@RequestMapping("/updateinfo")
	//修改个人信息
	public ModelAndView updateInfo(User user , ModelMap map) {
		//更新个人信息
		userServiceImpl.updateInfo(user);
		
		map.addAttribute("user",user);
		
		return new ModelAndView("redirect:/user/userinfo");		
	}
	
	@RequestMapping("/md5info")
	//修改个人信息
	public ModelAndView md5Info(User user , ModelMap map) {
		//加密用户密码
		String pwd = MD5Utils.MD5Encode(user.getUserPassword(), "UTF-8");
		user.setUserPassword(pwd);
		
		//更新个人信息
		userServiceImpl.updateInfo(user);
		
		map.addAttribute("user",user);
		
		return new ModelAndView("redirect:/user/userinfo");		
	}
	
	@RequestMapping("/verphone")
	@ResponseBody
	//ajax验证手机号
	public String verPhone(String phone) {
		//判断手机号是否存在
		boolean flag = userServiceImpl.verPhone(phone);
		
		if(flag) {
			//存在
			return "success";
		}
		//不存在
		return "failed";
	}
	
	@RequestMapping("/veranswer")
	@ResponseBody
	//ajax验证密保答案
	public String verAnswer(String answer,String phone) {
		//判断答案是否正确
		boolean flag = userServiceImpl.verAnswer(answer,phone);
		
		if(flag) {
			//答案正确
			return "success";
		}
		//答案不正确
		return "failed";
	}
	
	@RequestMapping("/verusername")
	@ResponseBody
	//ajax验证用户名
	public String verUsername(String username) {
		//用户名验证
		boolean flag = userServiceImpl.verUsername(username);
		
		if(flag) {
			//存在
			return "success";
		}
		//不存在
		return "failed";
	}
	
	@RequestMapping("/upload")
	public ModelAndView upload(MultipartFile imgFile,@ModelAttribute("user") User user,ModelMap map) {
		try {
			//获取文件上传名
			String fileName = imgFile.getOriginalFilename();
			
			//更新用户头像信息
			userServiceImpl.updateUserImg(fileName, user);
			user.setUserImg(fileName);
			map.addAttribute("user",user);
			
			//上传至虚拟路径D:/Imgproject/Joinlabs/userimg
			FileUtils.copyInputStreamToFile(
					imgFile.getInputStream(), 
					new File("D://Imgproject/Joinlabs/userimg/"+fileName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return new ModelAndView("redirect:/user/userimg");
	}
	
	@RequestMapping("/showaddress")
	//显示收获地址界面
	public ModelAndView showAddress(@ModelAttribute("user") User user,ModelMap map) {
		//查询用户地址
		List<Address> list = userServiceImpl.selectAddressByUserId(user.getUserId());
		
		map.addAttribute("list",list);
		map.addAttribute("addressNum",list.size());
		
		return new ModelAndView("/user/address");
	}
	
	@RequestMapping("/mycollect")
	//显示我的收藏界面
	public ModelAndView showCollect(@ModelAttribute("user") User user,ModelMap map) {
		//通过用户查询收藏列表
		List<Wares> list = userServiceImpl.selectEnshrineByUserId(user.getUserId());
		if(list.size()==0) {
			map.addAttribute("colNum",list.size());
		}
		map.addAttribute("list",list);
		return new ModelAndView("/user/collect");
	}
	
	@RequestMapping("/deladdress")
	//删除收获地址
	public ModelAndView delAddress(Long zyAid) {
		//进行删除地址操作
		int row = userServiceImpl.deleteAddrByAid(zyAid);
		
		return new ModelAndView("redirect:/user/showaddress");
	}
	
	@RequestMapping("/insertadd")
	//添加收获地址
	public ModelAndView addAddress(Address address) {
		//添加地址
		System.out.println(address.toString());
		userServiceImpl.addAddressByUserId(address);
		
		return new ModelAndView("redirect:/user/showaddress");
	}
	
	@RequestMapping("/updateadd")
	//编辑收获地址
	public ModelAndView updateAddress(Address address) {
		//编辑地址
		userServiceImpl.updateAddressById(address);
		
		return new ModelAndView("redirect:/user/showaddress");
	}
	
	@RequestMapping("/delcollect")
	//编辑收获地址
	public ModelAndView delCollect(String userId,BigDecimal waresId) {
		//取消收藏
		userServiceImpl.deleteCollect(userId,waresId);
		
		return new ModelAndView("redirect:/user/mycollect");
	}
}
